<?php
class ViewRender {
	const VIEW_TITLE = "title";
	const VIEW_PATH = "path";
	const VIEW_FILE = "file";
	const VIEW_REFER_PATH = "referpath";
	const VIEW_CSS_REFER = "cssrefer";
	const VIEW_JS_REFER = "jsrefer";
	const VIEW_CHARSET = "charset";
	const VIEW_CSS_FOLDER = "cssfolder";
	const VIEW_CSS_FILES = "cssfile";
	const VIEW_JS_FOLDER = "jsfolder";
	const VIEW_JS_FILES = "jsfile";
	const VIEW_IMAGE_FOLDER = "imagefoler";
	const VIEW_IMPORT_JS = "jsimports";
	const VIEW_IMPORT_CSS = "cssimports";
	const MES_TYPE_ERROR = "error";
	const MES_TYPE_INFO = "info";
	const MES_TYPE_WARNING = "warning";
	private $_layoutName;
	private $_hasLayout;
	private $_hasView;
	private $_module;
	private $_controller;
	private $_action;
	private $_view;
	private $_viewType;
	private $_viewPath;
	private $_controllerClass;
	/**
	 * HttpContext here .
	 *
	 * ..
	 *
	 * @var HttpContext
	 */
	private $_context;
	
	/**
	 * LayoutConfig.
	 *
	 * @var LayoutConfig
	 */
	private $_layoutConfig;
	protected $viewTitle;
	protected $_errors;
	protected $_message;
	protected $_messageType;
	protected $_hasError;
	public $searchData = null;
	public $searchResults = null;
	public $viewData = null;
	public $viewBag = null;
	public function __construct($context, $module, $controller, $action, $view, $viewType, $layoutName) {
		$this->_context = $context;
		$this->_module = $module;
		$this->_controller = $controller;
		$this->_controllerClass = $controller . ControllerFactory::_SUFFIX;
		$this->_action = $action;
		$this->_view = $view;
		$this->_viewType = $viewType;
		$this->_layoutName = $layoutName;
		$this->_hasLayout = true;
		$this->_hasView = true;
		$this->_layoutConfig = LayoutConfig::getInstance ( $this->_layoutName );
	}
	public function render() {
		if (! $this->_hasView)
			return;
		$this->_viewPath = Configuration::APPLICATION_FOLDER . SLASH . $this->_module . SLASH . Configuration::VIEW_FOLDER . SLASH . $this->_controller . SLASH . $this->_action . SLASH . $this->_view . SLASH . $this->_viewType . ".php";
		$layoutPath = Configuration::PUBLIC_FOLDER . SLASH . Configuration::LAYOUT_FOLDER . SLASH . $this->_layoutName . SLASH . $this->_layoutConfig->getConfig ( ViewRender::VIEW_FILE );
		if ($this->_hasLayout) {
			if (file_exists ( $layoutPath )) {
				require_once $layoutPath;
			} else {
				Log::error ( get_class ( $this ), $layoutPath . " View not found" );
				if (Configuration::DEBUG_MOD)
					throw new ViewException ( $layoutPath . " View not found" );
			}
		} else {
			if (file_exists ( $this->_viewPath )) {
				require_once $this->_viewPath;
			} else {
				$this->_viewPath = Configuration::APPLICATION_FOLDER . SLASH . $this->_module . SLASH . Configuration::VIEW_FOLDER . SLASH . AdministratorController::DEFAULT_CONTROLLER . SLASH . $this->_action . SLASH . $this->_view . SLASH . $this->_viewType . ".php";
				if (file_exists ( $this->_viewPath )) {
					require_once $this->_viewPath;
				} else {
					Log::error ( get_class ( $this ), $this->_viewPath . " View not found" );
					if (Configuration::DEBUG_MOD)
						throw new ViewException ( $this->_viewPath . " View not found" );
				}
			}
		}
	}
	public function content() {
		if (file_exists ( $this->_viewPath )) {
			require_once $this->_viewPath;
		} else {
			$this->_viewPath = Configuration::APPLICATION_FOLDER . SLASH . $this->_module . SLASH . Configuration::VIEW_FOLDER . SLASH . AdministratorController::DEFAULT_CONTROLLER . SLASH . $this->_action . SLASH . $this->_view . SLASH . $this->_viewType . ".php";
			if (file_exists ( $this->_viewPath )) {
				require_once $this->_viewPath;
			} else {
				Log::error ( get_class ( $this ), $this->_viewPath . " View not found" );
				if (Configuration::DEBUG_MOD)
					throw new ViewException ( $this->_viewPath . " View not found" );
			}
		}
	}
	public function block($name, $parram = null) {
		$block = BlockFactory::instance ( $name );
		$block->setParram ( $parram );
		$block->render ();
	}
	public function getViewTitle() {
		if ($this->viewTitle == "")
			return $this->_layoutConfig->getConfig ( ViewRender::VIEW_TITLE );
		return $this->viewTitle;
	}
	public function setViewTitle($viewTitle) {
		$this->viewTitle = $viewTitle;
	}
	public function viewTitle() {
		return '<title>' . $this->getViewTitle () . '</title>';
	}
	public function viewCSS() {
		$str = "";
		foreach ( $this->_layoutConfig->getConfig ( ViewRender::VIEW_CSS_FILES ) as $value ) {
			$str .= '<link rel="stylesheet" href="' . $this->_context->getBaseUrl () . $this->_layoutConfig->getConfig ( ViewRender::VIEW_PATH ) . $this->_layoutConfig->getConfig ( ViewRender::VIEW_CSS_FOLDER ) . $value . '"/>';
		}
		return $str;
	}
	public function viewCSSRefer() {
		$str = "";
		foreach ( $this->_layoutConfig->getConfig ( ViewRender::VIEW_CSS_REFER ) as $value ) {
			$str .= '<link rel="stylesheet" href="' . $this->_context->getBaseUrl () . $this->_layoutConfig->getConfig ( ViewRender::VIEW_REFER_PATH ) . $this->_layoutConfig->getConfig ( ViewRender::VIEW_CSS_FOLDER ) . $value . '"/>';
		}
		return $str;
	}
	public function viewJS() {
		$str = "";
		foreach ( $this->_layoutConfig->getConfig ( ViewRender::VIEW_JS_FILES ) as $value ) {
			$str .= '<script src="' . $this->_context->getBaseUrl () . $this->_layoutConfig->getConfig ( ViewRender::VIEW_PATH ) . $this->_layoutConfig->getConfig ( ViewRender::VIEW_JS_FOLDER ) . $value . '" type="text/javascript"></script>';
		}
		return $str;
	}
	public function viewCSSImports() {
		$str = "";
		$config = $this->_layoutConfig->getConfig ( ViewRender::VIEW_IMPORT_CSS );
		if ($config != STRING_EMPTY) {
			foreach ( $config as $value ) {
				$str .= '<link rel="stylesheet" href="' . $this->_context->getBaseUrl () . $value . '"/>';
			}
		}
		return $str;
	}
	public function viewJSImports() {
		$str = "";
		$config = $this->_layoutConfig->getConfig ( ViewRender::VIEW_IMPORT_JS );
		if ($config != STRING_EMPTY) {
			foreach ( $config as $value ) {
				$str .= '<script src="' . $this->_context->getBaseUrl () . $value . '" type="text/javascript"></script>';
			}
		}
		return $str;
	}
	public function viewJSRefer() {
		$str = "";
		foreach ( $this->_layoutConfig->getConfig ( ViewRender::VIEW_JS_REFER ) as $value ) {
			$str .= '<script src="' . $this->_context->getBaseUrl () . $this->_layoutConfig->getConfig ( ViewRender::VIEW_REFER_PATH ) . $this->_layoutConfig->getConfig ( ViewRender::VIEW_JS_FOLDER ) . $value . '" type="text/javascript"></script>';
		}
		return $str;
	}
	public function viewRawURL() {
		$str = '<script type="text/javascript">';
		$str .= 'AppConfigs.baseURL = "' . $this->getContext ()->getBaseUrl () . '"';
		$str .= '</script>';
		return $str;
	}
	public function viewMetaContent() {
		return '<meta http-equiv="Content-Type" content="text/html; charset=' . $this->_layoutConfig->getConfig ( ViewRender::VIEW_CHARSET ) . '">';
	}
	public function getCSSUrl() {
		return $this->_context->getBaseUrl () . $this->_layoutConfig->getConfig ( ViewRender::VIEW_PATH ) . $this->_layoutConfig->getConfig ( ViewRender::VIEW_CSS_FOLDER );
	}
	public function getJSUrl() {
		return $this->_context->getBaseUrl () . $this->_layoutConfig->getConfig ( ViewRender::VIEW_PATH ) . $this->_layoutConfig->getConfig ( ViewRender::VIEW_JS_FOLDER );
	}
	public function getImageUrl() {
		return $this->_context->getBaseUrl () . $this->_layoutConfig->getConfig ( ViewRender::VIEW_PATH ) . $this->_layoutConfig->getConfig ( ViewRender::VIEW_IMAGE_FOLDER );
	}
	public function getCSSReferUrl() {
		return $this->_context->getBaseUrl () . $this->_layoutConfig->getConfig ( ViewRender::VIEW_REFER_PATH ) . $this->_layoutConfig->getConfig ( ViewRender::VIEW_CSS_FOLDER );
	}
	public function getJSReferUrl() {
		return $this->_context->getBaseUrl () . $this->_layoutConfig->getConfig ( ViewRender::VIEW_REFER_PATH ) . $this->_layoutConfig->getConfig ( ViewRender::VIEW_JS_FOLDER );
	}
	public function getImageReferUrl() {
		return $this->_context->getBaseUrl () . $this->_layoutConfig->getConfig ( ViewRender::VIEW_REFER_PATH ) . $this->_layoutConfig->getConfig ( ViewRender::VIEW_IMAGE_FOLDER );
	}
	public function setNoLayout() {
		$this->_hasLayout = false;
	}
	public function setNoView() {
		$this->_hasView = false;
	}
	public function layoutTitle() {
		return $this->_layoutConfig->getConfig ( ViewRender::VIEW_TITLE );
	}
	public function getContext() {
		return $this->_context;
	}
	public function getModule() {
		return $this->_module;
	}
	public function setModule($module) {
		$this->_module = $module;
	}
	public function getController() {
		return $this->_controller;
	}
	public function getControllerName() {
		$max = strlen ( $this->_controllerClass );
		$trim = strlen ( ControllerFactory::_SUFFIX );
		$length = $max - $trim;
		return substr ( $this->_controllerClass, 0, $length );
	}
	public function setController($controller) {
		$this->_controller = $controller;
	}
	public function getAction() {
		return $this->_action;
	}
	public function setAction($action) {
		$this->_action = $action;
	}
	public function getView() {
		return $this->_view;
	}
	public function setView($view) {
		$this->_view = $view;
	}
	public function getViewType() {
		return $this->_viewType;
	}
	public function setViewType($viewType) {
		$this->_viewType = $viewType;
	}
	public function getLayoutName() {
		return $this->_layoutName;
	}
	/**
	 * Set Layout name.
	 *
	 * @param string $layoutName        	
	 */
	public function setLayoutName($layoutName) {
		$this->_layoutName = $layoutName;
		$this->_layoutConfig = LayoutConfig::getInstance ( $this->_layoutName );
	}
	public function hasError() {
		return $this->_hasError;
	}
	public function setHasError($hasError) {
		$this->_hasError = $hasError;
	}
	public function getError() {
		return $this->_errors;
	}
	public function setError($errors) {
		$this->_errors = $errors;
	}
	public function getMessage() {
		return $this->_message;
	}
	public function setMessage($message) {
		$this->_message = $message;
	}
	public function setMessageType($type) {
		$this->_messageType = $type;
	}
	public function getMessageType() {
		return $this->_messageType;
	}
	public function setErrorMessage($message) {
		$this->_messageType = ViewRender::MES_TYPE_ERROR;
		$this->_message = $message;
	}
	public function setInfoMessage($message) {
		$this->_messageType = ViewRender::MES_TYPE_INFO;
		$this->_message = $message;
	}
	public function setWaringMessage($message) {
		$this->_messageType = ViewRender::MES_TYPE_WARNING;
		$this->_message = $message;
	}
	/**
	 * Render View Message
	 */
	public function viewMessage() {
		if (isset ( $this->_message ) && $this->_message != "")
			echo '<div class="message ' . $this->_messageType . '">' . $this->_message . '</div>';
	}
	/**
	 * List Error Messages
	 */
	public function errorMessages() {
		if (isset ( $this->_errors )) {
			echo '<ul class="errors">';
			foreach ( $this->_errors as $error ) {
				echo '<li>' . $error . '</li>';
			}
			echo '</ul>';
		}
	}
	public function getViewDataValue($name) {
		return isset ( $this->viewData [$name] ) ? $this->viewData [$name] : null;
	}
	public function hidden($name, $class = null, $value = null, $resource = null, $id = null) {
		if (! isset ( $id ))
			$id = "";
		if (! isset ( $class ))
			$class = "";
		if (! isset ( $value ))
			$value = $this->getViewDataValue ( $name );
		if (! isset ( $value ))
			$value = "";
		$name = $this->getController () . "_" . $name;
		if (isset ( $resource )) {
			$resource = ResourceMessage::getInstance ()->getMessage ( $resource );
		} else {
			$resource = ResourceMessage::getInstance ()->getMessage ( $name );
		}
		echo '<div class="elemt ' . $class . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="lb">';
		echo '<input type="hidden" value="' . $value . '" name="' . $name . '" id="' . $id . '">';
		echo $value;
		echo '</div>';
		echo '</div>';
	}
	public function hiddenNoLabel($name, $value = null, $id = null) {
		if (! isset ( $id ))
			$id = "";
		if (! isset ( $value ))
			$value = $this->getViewDataValue ( $name );
		if (! isset ( $value ))
			$value = "";
		$name = $this->getController () . "_" . $name;
		echo '<input type="hidden" value="' . $value . '" name="' . $name . '" id="' . $id . '">';
	}
	public function renderPage() {
		$value = $this->getViewDataValue ( Constant::PAGE );
		if (! isset ( $value ))
			$value = "";
		echo '<input type="hidden" value="' . $value . '" name="' . Constant::PAGE . '">';
	}
	public function renderSize() {
		$value = $this->getViewDataValue ( Constant::SIZE );
		if (! isset ( $value ))
			$value = "";
		echo '<input type="hidden" value="' . $value . '" name="' . Constant::SIZE . '">';
	}
	public function renderOrderBy() {
		$value = $this->getViewDataValue ( Constant::ORDERBY );
		if (! isset ( $value ))
			$value = "";
		echo '<input type="hidden" value="' . $value . '" name="' . Constant::ORDERBY . '">';
	}
	public function renderOrderAs() {
		$value = $this->getViewDataValue ( Constant::ORDERAS );
		if (! isset ( $value ))
			$value = "";
		echo '<input type="hidden" value="' . $value . '" name="' . Constant::ORDERAS . '">';
	}
	public function textbox($name, $class = null, $validates = null, $value = null, $resource = null, $id = null) {
		if (! isset ( $id ))
			$id = "";
		if (! isset ( $class ))
			$class = "";
		if (! isset ( $value ))
			$value = $this->getViewDataValue ( $name );
		if (! isset ( $value ))
			$value = "";
		$name = $this->getController () . "_" . $name;
		if (isset ( $resource ))
			$resource = ResourceMessage::getInstance ()->getMessage ( $resource );
		else
			$resource = ResourceMessage::getInstance ()->getMessage ( $name );
		$placeholder = ResourceMessage::getInstance ()->getMessage2 ( "Msg_001", array (
				$resource 
		) );
		$valClass = "";
		if (isset ( $validates )) {
			foreach ( $validates as $val ) {
				$valClass .= ' ' . $val;
			}
		}
		echo '<div class="elemt ' . $class . ' ' . $valClass . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="ctr">';
		echo '<input placeholder="' . $placeholder . '" type="text" value="' . $value . '" name="' . $name . '" class="' . $valClass . '">';
		echo '</div>';
		echo '</div>';
	}
	public function textArea($name, $class = null, $validates = null, $value = null, $resource = null, $id = null) {
		if (! isset ( $id ))
			$id = "";
		if (! isset ( $class ))
			$class = "";
		if (! isset ( $value ))
			$value = $this->getViewDataValue ( $name );
		if (! isset ( $value ))
			$value = "";
		$name = $this->getController () . "_" . $name;
		if (isset ( $resource ))
			$resource = ResourceMessage::getInstance ()->getMessage ( $resource );
		else
			$resource = ResourceMessage::getInstance ()->getMessage ( $name );
		$valClass = "";
		if (isset ( $validates )) {
			foreach ( $validates as $val ) {
				$valClass .= ' ' . $val;
			}
		}
		echo '<div class="elemt ' . $class . ' ' . $valClass . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="ctr">';
		echo '<textarea name="' . $name . '" class="' . $valClass . '">' . $value . '</textarea>';
		echo '</div>';
		echo '</div>';
	}
	public function textEditor($name, $class = null, $value = null, $resource = null, $id = null) {
		if (! isset ( $id ))
			$id = "";
		if (! isset ( $class ))
			$class = "";
		if (! isset ( $value ))
			$value = $this->getViewDataValue ( $name );
		if (! isset ( $value ))
			$value = "";
		$name = $this->getController () . "_" . $name;
		if (isset ( $resource ))
			$resource = ResourceMessage::getInstance ()->getMessage ( $resource );
		else
			$resource = ResourceMessage::getInstance ()->getMessage ( $name );
		echo '<div class="elemt ' . $class . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="ctr">';
		echo '<textarea name="' . $name . '" class="ckeditor">' . $value . '</textarea>';
		echo '</div>';
		echo '</div>';
	}
	public function inlineEditor($name, $class = null, $value = null, $resource = null, $id = null) {
		if (! isset ( $id ))
			$id = "";
		if (! isset ( $class ))
			$class = "";
		if (! isset ( $value ))
			$value = $this->getViewDataValue ( $name );
		if (! isset ( $value ))
			$value = "";
		$name = $this->getController () . "_" . $name;
		if (isset ( $resource ))
			$resource = ResourceMessage::getInstance ()->getMessage ( $resource );
		else
			$resource = ResourceMessage::getInstance ()->getMessage ( $name );
		echo '<div class="elemt ' . $class . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="ctr">';
		echo '<textarea name="' . $name . '" contenteditable="true">' . $value . '</textarea>';
		echo '</div>';
		echo '</div>';
	}
	public function password($name, $class = null, $resource = null, $id = null) {
		if (! isset ( $id ))
			$id = "";
		if (! isset ( $class ))
			$class = "";
		$name = $this->getController () . "_" . $name;
		if (isset ( $resource ))
			$resource = ResourceMessage::getInstance ()->getMessage ( $resource );
		else
			$resource = ResourceMessage::getInstance ()->getMessage ( $name );
		$placeholder = ResourceMessage::getInstance ()->getMessage2 ( "Msg_001", array (
				$resource 
		) );
		echo '<div class="elemt ' . $class . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="ctr">';
		echo '<input placeholder="' . $placeholder . '" type="password" name="' . $name . '">';
		echo '</div>';
		echo '</div>';
	}
	public function imageEditUpload($name, $class = null, $value = null, $resource = null, $id = null) {
		if (! isset ( $id ))
			$id = "";
		if (! isset ( $class ))
			$class = "";
		if (! isset ( $value ))
			$value = $this->getViewDataValue ( $name );
		$name = $this->getController () . "_" . $name;
		if (isset ( $resource ))
			$resource = ResourceMessage::getInstance ()->getMessage ( $resource );
		else
			$resource = ResourceMessage::getInstance ()->getMessage ( $name );
		echo '<div class="elemt ' . $class . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="ctr">';
		if (isset ( $value ) && $value != "") {
			$imageUrl = URLUltil::getImageUrl ( $this->getContext (), strtolower ( $this->getController () ), $value );
			echo '<img class="form-img" src="' . $imageUrl . '">';
			echo '<input type="hidden" name="' . $name . '" value="' . $value . '">';
		}
		echo '<input type="file" name="' . $name . '">';
		echo '</div>';
		echo '</div>';
	}
	public function combobox($name, $datasource, $class = null, $value = null, $resource = null, $id = null) {
		if (! isset ( $id ))
			$id = "";
		if (! isset ( $class ))
			$class = "";
		if (! isset ( $value ))
			$value = $this->getViewDataValue ( $name );
		if (! isset ( $value ))
			$value = "";
		$name = $this->getController () . "_" . $name;
		if (isset ( $resource ))
			$resource = ResourceMessage::getInstance ()->getMessage ( $resource );
		else
			$resource = ResourceMessage::getInstance ()->getMessage ( $name );
		echo '<div class="elemt ' . $class . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="ctr">';
		echo '<select id="' . $id . '" name="' . $name . '">';
		foreach ( $datasource as $val ) {
			$selected = "";
			if ($val [Constant::COMBOBOX_VALUE] == $value)
				$selected = ' selected="selected"';
			echo '<option value="' . $val [Constant::COMBOBOX_VALUE] . '" ' . $selected . '>' . $val [Constant::COMBOBOX_TEXT] . '</option>';
		}
		echo '</select>';
		echo '</div>';
		echo '</div>';
	}
	public function imageURL($imageName, $folder = null) {
		return URLUltil::getImageUrl ( $this->getContext (), strtolower ( $this->getController () ), $imageName );
	}
	public function createBy($class = null) {
		if (! isset ( $class ))
			$class = "";
		$resource = ResourceMessage::getInstance ()->getMessage ( "System_createby" );
		$value = $this->getViewDataValue ( BaseEntity::CREATEBY );
		$nameVal = $this->getViewDataValue ( BaseEntity::CREATENAME );
		if (! isset ( $value ))
			$value = "";
		if (! isset ( $nameVal ))
			$nameVal = "";
		$name = $this->getController () . "_" . BaseEntity::CREATEBY;
		echo '<div class="elemt ' . $class . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="lb">';
		echo $nameVal;
		echo '<input type="hidden" value="' . $value . '" name="' . $name . '">';
		echo '</div>';
		echo '</div>';
	}
	public function updateBy($class = null) {
		if (! isset ( $class ))
			$class = "";
		$resource = ResourceMessage::getInstance ()->getMessage ( "System_lastupdateby" );
		$value = $this->getViewDataValue ( BaseEntity::LASTUPDATEBY );
		$nameVal = $this->getViewDataValue ( BaseEntity::LASTUPDATENAME );
		if (! isset ( $value ))
			$value = "";
		if (! isset ( $nameVal ))
			$nameVal = "";
		$name = $this->getController () . "_" . BaseEntity::LASTUPDATEBY;
		echo '<div class="elemt ' . $class . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="lb">';
		echo $nameVal;
		echo '<input type="hidden" value="' . $value . '" name="' . $name . '">';
		echo '</div>';
		echo '</div>';
	}
	public function createTime($class = null) {
		if (! isset ( $class ))
			$class = "";
		$resource = ResourceMessage::getInstance ()->getMessage ( "System_createtime" );
		$value = $this->getViewDataValue ( BaseEntity::CREATETIME );
		if (! isset ( $value ))
			$value = "";
		$name = $this->getController () . "_" . BaseEntity::CREATETIME;
		echo '<div class="elemt ' . $class . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="lb">';
		echo $value;
		echo '<input type="hidden" value="' . $value . '" name="' . $name . '">';
		echo '</div>';
		echo '</div>';
	}
	public function updateTime($class = null) {
		if (! isset ( $class ))
			$class = "";
		$resource = ResourceMessage::getInstance ()->getMessage ( "System_lastupdatetime" );
		$value = $this->getViewDataValue ( BaseEntity::LASTUPDATETIME );
		if (! isset ( $value ))
			$value = "";
		$name = $this->getController () . "_" . BaseEntity::LASTUPDATETIME;
		echo '<div class="elemt ' . $class . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="lb">';
		echo $value;
		echo '<input type="hidden" value="' . $value . '" name="' . $name . '">';
		echo '</div>';
		echo '</div>';
	}
	public function rowVerson($class = null) {
		if (! isset ( $class ))
			$class = "";
		$resource = ResourceMessage::getInstance ()->getMessage ( "System_rowversion" );
		$value = $this->getViewDataValue ( BaseEntity::ROWVERSION );
		if (! isset ( $value ))
			$value = "";
		$name = $this->getController () . "_" . BaseEntity::ROWVERSION;
		echo '<div class="elemt ' . $class . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="lb">';
		echo $value;
		echo '<input type="hidden" value="' . $value . '" name="' . $name . '">';
		echo '</div>';
		echo '</div>';
	}
	public function gender($name, $class = null, $resource = null, $id = null) {
		if (! isset ( $id ))
			$id = "";
		if (! isset ( $class ))
			$class = "";
		$value = $this->getViewDataValue ( $name );
		if (! isset ( $value ))
			$value = 0;
		$name = $this->getController () . "_" . $name;
		if (isset ( $resource ))
			$resource = ResourceMessage::getInstance ()->getMessage ( $resource );
		else
			$resource = ResourceMessage::getInstance ()->getMessage ( $name );
		echo '<div class="elemt ' . $class . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="ctr">';
		echo '<select name="' . $name . '">';
		if ($value != 0) {
			echo '<option value="0">Nữ</option>';
			echo '<option selected="selected" value="1">Nam</option>';
		} else {
			echo '<option selected="selected" value="0">Nữ</option>';
			echo '<option value="1">Nam</option>';
		}
		echo '</select>';
		echo '</div>';
		echo '</div>';
	}
	public function status($class = null, $id = null) {
		if (! isset ( $id ))
			$id = "";
		if (! isset ( $class ))
			$class = "";
		$value = $this->getViewDataValue ( BaseEntity::STATUS );
		if (! isset ( $value ))
			$value = 0;
		$name = $this->getController () . "_" . BaseEntity::STATUS;
		$resource = ResourceMessage::getInstance ()->getMessage ( "System_status" );
		echo '<div class="elemt ' . $class . '">';
		echo '<div class="lb">' . $resource . ':</div>';
		echo '<div class="ctr">';
		echo '<select name="' . $name . '" id="' . $id . '">';
		$db = DBHelper::getInstance ();
		$sql = 'select * from status';
		$results = $db->fetchAll ( $sql );
		foreach ( $results as $val ) {
			$selected = "";
			if ($val ['id'] == $value)
				$selected = 'selected="selected"';
			echo '<option value="' . $val ['id'] . '" ' . $selected . '>' . $val ['name'] . '</option>';
		}
		echo '</select>';
		echo '</div>';
		echo '</div>';
	}
	public function buttonNew($class = null) {
		if (! isset ( $class ))
			$class = "";
		$text = ResourceMessage::getInstance ()->getMessage ( "Button_New" );
		echo '<button class="bt btnew ' . $class . '" name="btNew" type="submit">' . $text . '</button>';
	}
	public function buttonSave($class = null) {
		if (! isset ( $class ))
			$class = "";
		$text = ResourceMessage::getInstance ()->getMessage ( "Button_Save" );
		echo '<button class="bt btsave ' . $class . '" name="btSave" type="submit">' . $text . '</button>';
	}
	public function buttonSearch($class = null) {
		if (! isset ( $class ))
			$class = "";
		$text = ResourceMessage::getInstance ()->getMessage ( "Button_Search" );
		echo '<button class="bt btsearch ' . $class . '" name="btSearch" type="submit">' . $text . '</button>';
	}
	public function addError($error) {
		if (! isset ( $this->_errors ))
			$this->_errors = array ();
		array_push ( $this->_errors, $error );
	}
	public function clearError() {
		$this->_errors = null;
	}
	public function renderPageSize($searchEntity) {
		$size = isset ( $searchEntity [Constant::SIZE] ) ? $searchEntity [Constant::SIZE] : Constant::PAGE_DEFAULT;
		$sel1 = $sel2 = $sel3 = $sel4 = $sel5 = "";
		switch ($size) {
			case '5' :
				$sel1 = 'selected="selected"';
				break;
			case '20' :
				$sel2 = 'selected="selected"';
				break;
			case '40' :
				$sel3 = 'selected="selected"';
				break;
			case '80' :
				$sel4 = 'selected="selected"';
				break;
			case '150' :
				$sel5 = 'selected="selected"';
				break;
			default :
				$sel1 = 'selected="selected"';
				break;
		}
		echo '<select name="size">';
		echo '<option value="5" ' . $sel1 . '>5</option>';
		echo '<option value="20" ' . $sel2 . '>20</option>';
		echo '<option value="40" ' . $sel3 . '>40</option>';
		echo '<option value="80" ' . $sel4 . '>80</option>';
		echo '<option value="150" ' . $sel5 . '>150</option>';
		echo '</select>';
	}
	public function renderSearchFoorter($searchEntity) {
		echo '<div class="nav">';
		$this->renderPageSize ( $searchEntity );
		$pageR = $this->generatePage ( $searchEntity );
		echo '</div>';
		$this->pageMessage ( $pageR [0], $pageR [1], $pageR [2], $pageR [3] );
	}
	private function generatePage($searchEntity) {
		$page = $searchEntity [Constant::PAGE];
		$size = $searchEntity [Constant::SIZE];
		$total = $searchEntity [Constant::TOTAL];
		$count = count ( $searchEntity [Constant::RESULTS] );
		$totalPage = ( int ) ($total / $size);
		if (($total % $size) != 0)
			$totalPage ++;
		if ($totalPage > 1) {
			echo '<ul class="pagenum">';
			echo '<li><a href="' . $this->pageLink ( 1, $size, $searchEntity) . '">Đầu</a></li>';
			for($i = 1; $i <= $totalPage; $i ++) {
				if ($i == $page)
					echo '<li class="current"><span>' . $i . '</span></li>';
				else
					echo '<li><a href="' . $this->pageLink ( $i, $size, $searchEntity ) . '">' . $i . '</a></li>';
			}
			echo '<li><a href="' . $this->pageLink ( $totalPage, $size, $searchEntity ) . '">Cuối</a></li>';
			echo '</ul>';
		}
		return array (
				$page,
				$size,
				$count,
				$total 
		);
	}
	private function pageLink($page, $size, $se) {
		$url = $this->getContext ()->getBaseUrl () . URLUltil::ADMIN . '/' . $this->_controller . '/search/default.html?' 
				. Constant::PAGE . '=' . $page . '&' 
				. Constant::SIZE . '=' . $size;
		if(isset($se[Constant::ORDERBY])){
			$url .= "&" . Constant::ORDERBY . "=" .$se[Constant::ORDERBY];
			$se[Constant::ORDERAS] = isset($se[Constant::ORDERAS])?$se[Constant::ORDERAS]:Constant::ORDERAS_DEFAULT;
			$url .= "&" . Constant::ORDERAS . "=" .$se[Constant::ORDERAS];
		}
		$se2 = $se;
		unset($se2[Constant::RESULTS]);
		unset($se2[Constant::TOTAL]);
		unset($se2[Constant::PAGE]);
		unset($se2[Constant::SIZE]);
		unset($se2[Constant::ORDERBY]);
		unset($se2[Constant::ORDERAS]);
		foreach ($se2 as $key=> $value) {
			if(isset($value) && $value != ""){
				$kname = $this->_controller . "_" . $key;
				$url .= "&" . $kname . "=" .$value;
			}
		}
		return $url;
	}
	private function pageMessage($page, $size, $count, $total) {
		$from = (($page - 1) * $size) + 1;
		$to = $from + $count - 1;
		$msg = "";
		if($total == 0)
			$msg = ResourceMessage::getInstance ()->getMessage("Msg_006");
		else
			$msg = ResourceMessage::getInstance ()->getMessage2 ( "Msg_005", array (
					$from,
					$to,
					$total 
			) );
		echo '<div class="info">' . $msg . '</div>';
	}
}